package export

import (
	"strconv"

	userSdk "github.com/wangzhe1991/grpc-sdk/pb/user"

	"gitee.com/krio/grpc-go-server/cmd/lib"
	"gitee.com/krio/grpc-go-server/tool"
)

// 导出
func UserExport(title string, titleS []string, total int, list []*userSdk.UserListResp_One) (*userSdk.UserListExportResp, error) {
	sheetName := title // 工作表
	e := tool.NewExcelize(sheetName, titleS, total)
	// 创建一个工作表
	index := e.File.NewSheet(sheetName)
	// 设置工作簿的默认工作表
	e.File.SetActiveSheet(index)
	// 设置大标题
	e = e.TitleCellMerge(title)
	// 设置样式
	e = e.SetExampleStyle()
	// 设置列标题
	e = e.SetColTitle()

	// 设置列宽度
	// e.File.SetColWidth(sheetName, "A", "B", 20)
	// e.File.SetColWidth(sheetName, "C", "D", 40)

	// 设置单元格的值
	for i := 0; i < total; i++ {
		sI := strconv.Itoa(i + 3)
		e.File.SetCellValue(sheetName, ("A" + sI), list[i].Id)
		e.File.SetCellValue(sheetName, ("A" + sI), list[i].Sfid)
		e.File.SetCellValue(sheetName, ("A" + sI), list[i].PhoneAreaCode)
		e.File.SetCellValue(sheetName, ("A" + sI), list[i].PhoneNumber)
		e.File.SetCellValue(sheetName, ("A" + sI), list[i].FirstName)
		e.File.SetCellValue(sheetName, ("B" + sI), list[i].LastName)
		e.File.SetCellValue(sheetName, ("A" + sI), list[i].Email)
	}

	// Save spreadsheet by the given path.
	res, err := e.FileSaveAs(lib.C.File.Driver)
	if err != nil {
		return nil, err
	}

	return &userSdk.UserListExportResp{
		Path:  res.URL,
		File:  res.FileName,
		Title: title,
	}, nil
}
